<template>
    <el-dialog v-dialogDrag :title="!dataForm.pkCategory ? '新增' : '修改'" :close-on-click-modal="false"
        :before-close="closeDailog" :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
            <el-form-item label="分类名称" prop="categoryName">
                <el-input v-model="dataForm.categoryName" placeholder="分类名称" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="分类编码" prop="categoryCode">
                <el-input v-model="dataForm.categoryCode" placeholder="分类编码" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="分类级别" prop="categoryLevel">
                <ec-dict-select defaultValue="1" dictType="category_level" v-model="dataForm.categoryLevel"
                    placeholder="分类级别"></ec-dict-select>
            </el-form-item>
            <el-form-item label="分类图片" prop="categoryIcon">
                <ec-form-single-upload v-model="dataForm.categoryIconObj" :dataForm="{fileSpace: 'mall',
                    storeType: 'local'}"></ec-form-single-upload>
            </el-form-item>
            <el-form-item label="分类排序" prop="categorySort">
                <el-input v-model="dataForm.categorySort" placeholder="分类排序" maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="分类状态" prop="categoryStatus">
                <ec-dict-select defaultValue="1" dictType="enable_status" v-model="dataForm.categoryStatus"
                    placeholder="分类状态"></ec-dict-select>
            </el-form-item>
            <el-form-item label="父级分类" prop="pid">
                <el-input v-model="dataForm.pid" placeholder="父级分类" maxlength="30" show-word-limit></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog()">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as goodcategoryService from '@/api/mall/goodcategory'

    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    pkCategory: 0,
                    categoryName: '',
                    categoryCode: '',
                    categoryLevel: '',
                    categoryIcon: '',
                    categoryIconObj: {},
                    categorySort: '',
                    categoryStatus: '',
                    pid: '',
                    revision: '',
                    createdBy: '',
                    createdTime: '',
                    updatedBy: '',
                    updatedTime: '',
                    deleFlag: ''
                },
                dataRule: {
                    categoryName: [{
                        required: true,
                        message: '分类名称不能为空',
                        trigger: 'blur'
                    }],
                    categoryCode: [{
                        required: true,
                        message: '分类编码不能为空',
                        trigger: 'blur'
                    }],
                    categoryLevel: [{
                        required: true,
                        message: '分类级别不能为空',
                        trigger: 'blur'
                    }],
                    categoryIcon: [{
                        required: true,
                        message: '分类图片不能为空',
                        trigger: 'blur'
                    }],
                    categorySort: [{
                        required: true,
                        message: '分类排序不能为空',
                        trigger: 'blur'
                    }],
                    categoryStatus: [{
                        required: true,
                        message: '分类状态不能为空',
                        trigger: 'blur'
                    }],
                    pid: [{
                        required: true,
                        message: '父级分类不能为空',
                        trigger: 'blur'
                    }],
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.pkCategory = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.pkCategory) {
                        goodcategoryService.getGoodCategoryInfo(this.dataForm.pkCategory).then(data => {
                            this.dataForm.categoryName = data.categoryName
                            this.dataForm.categoryCode = data.categoryCode
                            this.dataForm.categoryLevel = data.categoryLevel
                            this.dataForm.categoryIcon = data.categoryIcon
                            this.dataForm.categorySort = data.categorySort
                            this.dataForm.categoryStatus = data.categoryStatus
                            this.dataForm.pid = data.pid
                            this.dataForm.revision = data.revision

                            // 图片对象
                            if (data.categoryIconObj) {
                                this.dataForm.categoryIconObj = data.categoryIconObj
                            }else{
                                this.dataForm.categoryIconObj = {
                                    
                                }
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'pkCategory': this.dataForm.pkCategory || undefined,
                            'categoryName': this.dataForm.categoryName,
                            'categoryCode': this.dataForm.categoryCode,
                            'categoryLevel': this.dataForm.categoryLevel,
                            'categoryIcon': this.dataForm.categoryIconObj.id,
                            'categorySort': this.dataForm.categorySort,
                            'categoryStatus': this.dataForm.categoryStatus,
                            'pid': this.dataForm.pid,
                            'revision': this.dataForm.revision,
                        }
                        var tick = !this.dataForm.pkCategory ? goodcategoryService.addGoodCategory(params) :
                            goodcategoryService.updateGoodCategory(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.visible = false
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            }
        }
    }
</script>